package utils
{
	import flash.utils.describeType;

	/**
	 * 对象处理工具
	 */	
	public class ObjectUtil
	{
		/**
		 *动态对象属性快速拷贝 
		 * @param source: 数据源，必须支持for in访问
		 * @param target：目标对象
		 * 
		 */		
		public static function foo(source:Object,target:Object):void
		{
			for(var key:String in source)
			{
				if(target.hasOwnProperty(key))
					target[key] = source[key];
			}
		}
		
		/**
		 * 对象属性拷贝
		 *  
		 * @param source 源对象
		 * @param target 目标对象
		 */		
		public static function foo1(source:Object,target:Object):void
		{
			var xml:XML = describeType(source);
			for(var i:int =0;i<xml.variable.length();i++)
			{
				var name:String = xml.variable[i].@name+"";//获取类的属性名
				if(target.hasOwnProperty(name) && source.hasOwnProperty(name))
					source[name] = target[name];
			}
		}
		
		
		/**
		 * 转 obj 对象
		 * @param source  源对象
		 * @return Object 对象
		 */			
		public static function foo2(source:Object):Object
		{
			var a:Object = new Object();
			var xml:XML = describeType(source);
			for(var i:int =0;i<xml.variable.length();i++)
			{
				var name:String = xml.variable[i].@name+"";//获取类的属性名
				a[name] = source[name];
			}
			
			return a;
		}
		
		/*
		 * 克隆对象
		 * @param source 源对象
		 * @return 新对象
		 	
		public static function toObject(source:Object):Object
		{
			var d:ByteArrayAsset = new ByteArrayAsset();
			d.writeObject( source );
			d.position = 0;
			return d.readObject();
		}*/	
	}
}